﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FunMazeEngine
{
    [Serializable]
    public class Map
    {
        object locker = new object();
        public int rows = 0;
        public int columns = 0;
        public int cellNum = 0;
        public int treasureCount = 0;
        public int[,] map;
//        int[,] playermap;
        public Dictionary<int, int> treasureList = new Dictionary<int, int>();
        

        public void CreateMap(int numOfRowsColumns, int numOfTreasure)
        {
            this.rows = numOfRowsColumns;
            this.columns = numOfRowsColumns;
            this.treasureCount = numOfTreasure;

            map = new int[rows, columns];

            for (int xCounter = 0; xCounter < rows; xCounter++)
            {
                for (int yCounter = 0; yCounter < columns; yCounter++)
                {
                    cellNum++;
                    map[xCounter,yCounter] = cellNum;   
                }
            }

            SetTreasure();
        }

        private void SetTreasure()
        {
            Random random = new Random();
            int minCount = 0;
            int avgTreasureCount = (treasureCount / cellNum)+1;
            int maxCount = (avgTreasureCount * 3);
            int remainingTreasure = treasureCount;
            int maxTreasure = 0;

            //int avgTreasureCountForRow = treasureCount / rows;
            //int treasureForRow = 0;
            int randomTreasure = 0;

            // Converted back to previous idea, though it might not evenly spread, it is more likely
            // to reach the required treasure count.
            // Your idea of treasure per row is good, just that the avgTreasureCount may not be
            // an integer, so it can have a different number of treasure than specified.
            for (int x = 0; x < rows; x++)
            {
                //treasureForRow = avgTreasureCountForRow;
                for (int y = 0; y < columns; y++)
                {
                    /*maxCount = Math.Min(maxCount, treasureForRow);     
                    randomTreasure = random.Next(minCount, maxCount);
                    treasureList.Add(map[x,y], randomTreasure);
                    treasureForRow = treasureForRow - randomTreasure;*/
                    if (x == rows-1 && y == columns-1) {
                        treasureList.Add(map[x, y], remainingTreasure); remainingTreasure = 0;
                    }
                    else
                    {
                        maxTreasure = Math.Min(maxCount, remainingTreasure);
                        randomTreasure = random.Next(minCount, maxTreasure);
                        treasureList.Add(map[x, y], randomTreasure);
                        remainingTreasure = remainingTreasure - randomTreasure;
                    }
                }
            }
        }

        public Dictionary<int, int> GetTreasure()
        {
            return treasureList;
        }

        public int numberoftreasure(int row, int column)
        {
            return treasureList[map[row, column]];
        }
        public int PlayerGetTreasure(GamePlayer player, int row, int column)
        {

            lock (locker)
            {
                int TreasureObtained = treasureList[map[row, column]];
                player.AddTreasure(treasureList[map[row, column]]);
                treasureList[map[row, column]] = 0;
                return TreasureObtained;
            }
        }

        public void GetRowColumnLocation(int cellNum, out int theRow, out int theColumn)
        {
            int row = (int) cellNum % rows;
            int column = (int) cellNum / columns;

            theRow = row;
            theColumn = column;
        }

        public int GetCellLocation(int row, int column)
        {
            int cell = column * columns + row;

            return cell;
        }

        public int GetTotalCells()
        {
            return cellNum;
        }
    }

    
}
